package org.ichow.scenegame.core
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.text.TextField;

	/**
	 * ...
	 * @author M.ichow
	 */
	public class ToolsBar extends Sprite
	{
		static public const HELP : String = "help";
		static public const CLOSE : String = "close";
		static public const SOUND : String = "sound";

		private var _count : int = 0;
		private var _score : int = 0;
		private var _maxScore : int = 0;
		private var _isSound : Boolean = true;
		
		private var _tools:Array;
		//
		private var __countMc : Sprite;
		private var __countTf : TextField;
		private var __scoreMc : Sprite;
		private var __scoreTf : TextField;
		private var __helpMc : Sprite;
		private var __soundMc : MovieClip;
		private var __closeBtn : SimpleButton;
		private var __bar : Sprite;
		
		private var __bg:Sprite;

		public function ToolsBar( bar : DisplayObject )
		{
			if ( !bar )
				return;
			__bar = bar as Sprite;
			init();
		}
		
		/**
		 * 初始化工具条
		 */
		private function init() : void
		{
			//get 
			__countMc = __bar.getChildByName( "count_mc" ) as Sprite;
			__countTf = __countMc.getChildByName( "txt" ) as TextField;
			__scoreMc = __bar.getChildByName("score_mc") as Sprite;
			__scoreTf = __scoreMc.getChildByName( "txt" ) as TextField;
			__helpMc = __bar.getChildByName( "help_mc" ) as Sprite
			__soundMc = __bar.getChildByName( "sound_mc" ) as MovieClip;
			__closeBtn = __bar.getChildByName( "close_btn" ) as SimpleButton;
			__bg = __bar.getChildByName("bg_mc") as Sprite;
			
			//add listeners
			__helpMc.addEventListener( MouseEvent.CLICK , onHelp );
			__soundMc.addEventListener( MouseEvent.CLICK , onSound );
			__closeBtn.addEventListener( MouseEvent.CLICK , onClose );
			__helpMc.buttonMode = true;
			__soundMc.buttonMode = true;
				
			addChild(__bar);
			
			_tools = [__countMc , __scoreMc , __helpMc , __soundMc , __closeBtn];
		}
		
		override public function get width():Number 
		{
			return __bg.width;
		}
		
		override public function set width(value:Number):void 
		{
			__bg.width = value;
			//set ....
			var dis:int = 0;
			for (var i:int = 0, len:int = _tools.length; i < len; i++)
			{
				dis += _tools[i].width;
			}
			var d:int = Math.floor((__bg.width - dis) / (len + 1));
			var posx:int = d;
			for (i = 0; i < len; i++)
			{
				var t:DisplayObject = _tools[i];
				t.x = posx;
				posx += t.width + d;
			}
		}

		public function addCount( i : int = 1 ) : int
		{
			_count += i;
			__countTf.text = _count.toString();
			return _count;
		}

		/**
		 * 重新开始
		 */
		public function reset() : void
		{
			addCount( -_count );
			addScore( -_score );
		}

		/**
		 * 设置分数
		 * @param	v
		 */
		public function addScore( v : int ) : void
		{
			_score += v;
			__scoreTf.text = _score.toString();
			if (_score > _maxScore )
				_maxScore = _score;
		}

		/**
		 * 关闭
		 * @param	e
		 */
		private function onClose( e : MouseEvent ) : void
		{
			dispatchEvent( new Event( CLOSE ) );
		}

		/**
		 * 声音控制
		 * @param	e
		 */
		private function onSound( e : MouseEvent ) : void
		{
			_isSound = !_isSound;
			__soundMc.gotoAndStop( _isSound?1:2 );
			dispatchEvent( new Event( SOUND ) );
		}

		/**
		 * 帮助
		 * @param	e
		 */
		private function onHelp( e : MouseEvent ) : void
		{
			dispatchEvent( new Event( HELP ) );
		}
		
		public function get maxScore():int 
		{
			return _maxScore;
		}
		
		public function get score():int 
		{
			return _score;
		}
		
		public function get count():int 
		{
			return _count;
		}
		
		public function get isSound():Boolean 
		{
			return _isSound;
		}
		
		public function set isOver( v:Boolean ):void
		{
			__countMc.visible = !v;
			__scoreMc.visible = !v;
			__helpMc.visible = !v;
		}

	}

}